<template>
  <div>
    <div id="billChart" ref="billChart" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      pieChartInstane: '',
      billList: []
    }
  },
  create() {},
  mounted() {
    this.getLastMonthOfBills()
  },
  methods: {
    initPieChart() {
      this.pieChartInstane = this.$echarts.init(this.$refs.billChart)

      console.log(this.billList)
      const option = {
        title: {
          text: this.billList.title,
          subtext: this.billList.subtext,
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          data: this.billList.legend
        },
        series: [
          {
            name: '账单',
            type: 'pie',
            radius: '50%',
            data: this.billList.billValueDTOs,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              },
              label: {
                show: true
              },
              labelLine: {
                show: false
              }
            }
          }
        ]
      }
      this.pieChartInstane.setOption(option)
    },

    async getLastMonthOfBills() {
      const userId = window.sessionStorage.getItem('token')
      const { data: res } = await this.$http.get(
        '/home/getLastMonthOfBills?userId=' + userId
      )
      console.log(res)
      if (res.code !== 1) {
        return this.$message.error('获取生活费用失败！')
      }
      this.billList = res.data

      this.initPieChart()
    }
  }
}
</script>

<style scoped>
#billChart {
  width: 500px;
  height: 400px;
}
</style>
